package main;

import static main.Constants.*;
import static utils.Utils.*;
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import utils.Utils;

public final class Main extends Thread {

	static Logger logger = Logger.getLogger(Main.class);
	static int possision = 0;

	public static Robot robo;

	static Rectangle close;
	private Enemies enemy = null;
	private int countBattle = 0;

	static {
		try {
			robo = new Robot();
			PropertyConfigurator.configure("log4j.properties");
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	public Main(Enemies en, int val) {
		enemy = en;
		countBattle = val;

	}

	public void run() {

		// int wrong = 0;

		// logger.info("Start ------------------------------------------------------------");
		Utils.println("Begin program");
		setName("BOTTT THread!");
		// while(true)
		// try {
		// Thread.sleep(1500);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
			try {
				BOT_ON(enemy, countBattle);
				Runtime.getRuntime().exec("shutdown -s -t 15");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.exit(1);
		// this.stop();

	}

	static void BOT_ON(Enemies enemyType, int countBattle) throws InterruptedException {

		int pic = 0;
		for (int i = 0; i < countBattle; i++) {
			// Runtime.getRuntime().exec("shutdown -s -t " + countBattle*60);
			logger.info("battle#" + i);
			Utils.println("Battle #" + i + " " + GregorianCalendar.getInstance().getTime());
			logger.info("end regeneration");
			pic = 0;
			BufferedImage im = robo.createScreenCapture(enemyType.enRect);

			possision++; // find enemy

			Point p = target(enemyType, im);//Utils.searcByMovie(enemyType.enRect, 8);//
			if (p == null) {
				Utils.println("BAD PICtire" + (pic++));
				logger.warn("bad pic " + pic);
				i--;
				possision--;
				// Thread.sleep(3000);
				continue;
			}
			robo.mouseMove(p.x, p.y - enemyType.correctY);
			// if(true) {i--; continue;}
			LKM(10);
			Thread.sleep(60);
			robo.mouseMove((int) MouseInfo.getPointerInfo().getLocation().getX() + 100, (int) MouseInfo.getPointerInfo()
					.getLocation().getY() + 45);
			LKM(10);
			Thread.sleep(8000);
			if (!isAttacBegan()) {
				i--;
				possision--;
				Utils.println("---------NOT CLEAR TARGET");
				logger.warn("Attack not begun!!!");
				continue;
			}
			Utils.println("BATTLE -START" + " " + GregorianCalendar.getInstance().getTime());
			logger.info("start battle");
			robo.mouseMove(0, 0);
			// BufferedImage im2 = robo.createScreenCapture(LONG_HEALTH);
			// int udar = 0;
			possision++; // battle
			long time = System.currentTimeMillis();
			while (true) {

				while (isReadyAttack()) {
					if (useCell(Cells.allCells.get("l1").recB, 120))
						println("HP UP");
					robo.mouseMove((int) ATTACK.getCenterX(), (int) ATTACK.getCenterY());
					LKM(10);
					// robo.delay(100);
					robo.mouseMove((int) ATTACK.getCenterX(), (int) ATTACK.getCenterY() - 50);
					Thread.sleep(4000);
					// udar++;
				}
				if ((close = isReadyClose()) != null) {
					robo.mouseMove((int) close.getCenterX(), (int) close.getCenterY());
					LKM(100);
					Utils.println("BATTLE-END #" + i + " " + GregorianCalendar.getInstance().getTime());
					logger.info("end battle");
					// Thread.sleep(2400);
					if (Utils.searcByMovie(LONG_HEALTH,0) == null)
						Utils.refresh();
					break;
				}
				if(checkTime(time))
				{
					Utils.refresh();
					break;
				}
			}
			Utils.println("regeneration - START");

			boolean carrotFlag = true;
			possision++; // regeneration
			long timeRegeneration = System.currentTimeMillis();
			while (!isReadyHealth()) {
				if (carrotFlag) {
					useCell(Cells.allCells.get("r4").recN, 420);
					carrotFlag = false;
				}
				Thread.sleep(1000);// = 1sec
			}
			Utils.println("STOP regeneration time = " + (System.currentTimeMillis() - timeRegeneration) / 1000 + "sec.");
		}

	}

	private static boolean checkTime(long time) {
		if ((System.currentTimeMillis()-time)/1000 > 3*60) return true;
		return false;
	}

}
